﻿using System.Text.RegularExpressions;

using Storm.TextEditor.Editor;
using Storm.TextEditor.Parser.Objects;

namespace Storm.TextEditor.Parser
{
    /// <summary>
    /// Contains methods for parsing.
    /// </summary>
    public sealed class ParseTools
    {
        #region Fields

        private static Regex WordSplitter = new Regex(@"(?<space>\s)|(?<tab>\t)", RegexOptions.Compiled);

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Adds a word with the given text to the given row.
        /// </summary>
        /// <param name="text">Text of the word.</param>
        /// <param name="row">Row to add the word to.</param>
        /// <param name="pattern">Pattern of the word.</param>
        /// <param name="style">TextStyle of the word.</param>
        /// <param name="segment">Segment of the word.</param>
        /// <param name="hasError">Whether the word should have an error.</param>
        public static void AddPatternString(string text, Row row, Pattern pattern, TextStyle style, Segment segment, bool hasError)
        {
            Word word = row.Add(text);

            word.Style = style;
            word.Pattern = pattern;
            word.HasError = hasError;
            word.Segment = segment;
        }

        /// <summary>
        /// Adds the given text to the given row.
        /// </summary>
        /// <param name="text">Text to add.</param>
        /// <param name="row">Row to add the text to.</param>
        /// <param name="style">TextStyle of the text.</param>
        /// <param name="segment">Segment of the text.</param>
        public unsafe static void AddString(string text, Row row, TextStyle style, Segment segment)
        {
            if (text == "")
                return;

            string[] splittedText = WordSplitter.Split(text);
            string lineText = "";

            for (int i = 0; i < splittedText.Length; i++)
            {
                lineText = splittedText[i];
                Word word = row.Add(lineText);

                word.Style = style;
                word.Segment = segment;

                if (lineText == " ")
                    word.Type = WordType.Space;
                else if (lineText == "\t")
                    word.Type = WordType.Tab;
            }
        }

        #endregion

        #endregion
    }
}
